package com.visikard.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import com.visikard.application.Constant;
import com.visikard.application.UIMessage;
import com.visikard.application.VKRegApplication;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.GetRTUICouponTask;
import com.visikard.item.ItemRTUICoupon;
import com.visikard.ui.R;
import com.visikard.ui.activity.RequestRTUICouponActivity;
import com.visikard.ui.adapter.RTUICouponAdapter;
import com.visikard.utility.Utils;

public class ListCouponsRTUIFragment extends BaseFragment implements View.OnClickListener {

	private ListView listView;
	private Button btnBack, btnRequest;

	private Context mContext;
	ArrayList<ItemRTUICoupon> mListCoupons;
	private RTUICouponAdapter couponsAdapter;

	BaseTask mAsyncTask;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.rtui_coupon_main_fragment, container, false);
		mContext = getActivity();
		Utils.overrideFonts(view, Constant.FONT_GOTHAM_BOOK);
		innitControl(view);
		getListCoupons();
		return view;
	}

	private void innitControl(View view) {
		btnBack = (Button) view.findViewById(R.id.rtui_btn_back);
		btnRequest = (Button) view.findViewById(R.id.rtui_btn_request_coupon);
		listView = (ListView) view.findViewById(R.id.rtui_list_coupon);

		btnBack.setOnClickListener(this);
		btnRequest.setOnClickListener(this);

		mListCoupons = new ArrayList<ItemRTUICoupon>();
		couponsAdapter = new RTUICouponAdapter(mContext, mListCoupons);
		listView.setAdapter(couponsAdapter);
	}

	private void getListCoupons() {
		mAsyncTask = new GetRTUICouponTask(mHandler);
		mAsyncTask.execute(VKRegApplication.merchantAccountObj.getMerchantIdFromRTUI());
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			getListCoupons();
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.rtui_btn_back:
				showExitDialog();
				return;
			case R.id.rtui_btn_request_coupon:
				Intent intent = new Intent(getActivity(), RequestRTUICouponActivity.class);
				// startActivityForResult(intent, CouponBuilderActivity.REQUEST_CODE_CREATE_UPDATE_COUPON);
				startActivity(intent);
				return;
		}
		super.onClick(v);
	}

	private void showExitDialog() {
		AlertDialog dialog = new AlertDialog.Builder(mContext).setTitle(R.string.main_screen_dlg_exit_title)
				.setMessage(R.string.main_screen_dlg_exit_msg)
				.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (getActivity() != null)
							getActivity().finish();
					}
				}).setNegativeButton(getString(android.R.string.no), null).create();
		dialog.show();
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean handleMessage(Message msg) {
		if (getActivity() == null)
			return false;
		switch (msg.what) {
			case UIMessage.MSG_GET_LIST_SUCCESS:
				List<ItemRTUICoupon> listResults = (ArrayList<ItemRTUICoupon>) msg.obj;
				mListCoupons.addAll(listResults);
				couponsAdapter.notifyDataSetChanged();
				return true;
		}
		return super.handleMessage(msg);
	}

}
